package language;

import java.sql.*;

/**
 * URL是用于连接数据库的标识符
 * USER_NAME 是安装MySQL时使用的用户名
 * PASSWORD 是与用户名相对应的密码
 */
public class JdbcTest {

//    public static final String URL = "jdbc:mysql://localhost/dpsp?" + "user=root&password=root&serverTimezone=GMT";
    public static final String URL = "jdbc:mysql://localhost/test?" + "user=root&password=root&serverTimezone=GMT";
    public static final String SQL = "select * from book";
    public static void main(String[] args) {

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try (Connection conn = DriverManager.getConnection(URL);
             PreparedStatement pstmt = (PreparedStatement)conn.prepareStatement(SQL);) {
            ResultSet rs = pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("============================");
            while (rs.next()) {
                for (int i = 1; i <= col; i++) {
                    System.out.print(rs.getString(i) + "\t");
                    if ((i == 2) && (rs.getString(i).length() < 8)) {
                        System.out.print("\t");
                    }
                }
                System.out.println("");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}